package com.rpp.dao;

import com.rpp.io.Resource;
import com.rpp.pojo.User;
import com.rpp.session.SqlSession;
import com.rpp.session.SqlSessionFactory;
import com.rpp.session.SqlSesssionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class UserDao {

    public List<User> selectList() throws Exception {
        //加载资源
        InputStream inputStream = Resource.loadResource("sqlMapConfig.xml");
        //读取配置
        SqlSessionFactory build = SqlSesssionFactoryBuilder.build(inputStream);
        //建立会话
        SqlSession sqlSession = build.openSqlSession();

        List<User> list = sqlSession.selectList("com.rpp.mapper.UserMapper.selectList", null);
        return list;
    }

    public User selectOne() throws Exception {

        //加载资源
        InputStream inputStream = Resource.loadResource("sqlMapConfig.xml");
        //读取配置
        SqlSessionFactory build = SqlSesssionFactoryBuilder.build(inputStream);
        //建立会话
        SqlSession sqlSession = build.openSqlSession();

        //单个查询
        User user = new User();
        user.setId(1);
        user.setName("张三");
        User user1 = sqlSession.selectOne("com.rpp.mapper.UserMapper.selectOne", user);
        return user1;
    }
}
